//
//  DrivesModel.m
//  carcareIOS
//
//  Created by tianxing on 16/3/4.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "DrivesModel.h"
#import "MapReGeocodeSimple.h"
#import "CalendarMgr.h"

@implementation DrivesModel

-(void)setTravels:(NSArray *)travels{
    _travels = travels;
    NSMutableArray *models = [NSMutableArray arrayWithCapacity:travels.count];
    
    NSInteger mark = 0;
    NSInteger miles = 0;
    NSInteger time = 0;
    CGFloat fuelCost = 0;
    
    for (int i = 0; i < travels.count; i++) {
        OneDriveModel *model = [[OneDriveModel alloc] initWithDic:travels[i]];
        [models addObject:[[OneDriveModel alloc] initWithDic:travels[i]]];
        mark += model.mark;
        miles += model.mile;
        time += model.travelTime;
        fuelCost += model.fuelCost;
    }
    self.travelModels = models;
    
    if (travels.count > 0) {
        self.dayMark = mark / travels.count;
        self.dayMiles = miles;
        self.dayTime = time;
        self.dayFuelCost = fuelCost;
    }
    
}

-(NSInteger)drivingMark{
    if (_drivingMark == 0) {
        return self.dayMark;
    }
    return _drivingMark;
}

-(CGFloat)drivingMiles{
    if (!_drivingMiles) {
        return self.dayMiles;
    }
    return _drivingMiles;
}

-(NSInteger)drivingTime{
    if (!_drivingTime) {
        return self.dayTime;
    }
    return _drivingTime;
}


@end

NSString * const DRIVE_PLACE_UPDATE = @"DRIVE_PLACE_UPDATE";


@interface OneDriveModel()

@property (nonatomic , strong) MapReGeocodeSimple * mapSimpleStart;
@property (nonatomic , strong) MapReGeocodeSimple * mapSimpleEnd;

@end

@implementation OneDriveModel

- (void)setGpsTrackFlag:(NSInteger)gpsTrackFlag
{
    _gpsTrackFlag = gpsTrackFlag;
    if (gpsTrackFlag > 0) {
        self.gpsTrackBool = YES;
    }else self.gpsTrackBool = NO;
}
-(void)setTravelTime:(NSInteger)travelTime{
    _travelTime = travelTime / 60;
    NSInteger hour = _travelTime / 60;
    self.driveCarLongControl = hour < 2 ? 1 : (hour >= 2 && hour < 4 ? 2 : 3);
}
- (void)setStartTime:(NSString *)startTime
{
    _startTime = startTime;
    self.startTimeGps = [CALENDARMGR dateStrWithDate:[CALENDARMGR dateWithStr:startTime format:@"yyyy-MM-dd HH:mm:ss"] format:@"yyyyMMddHHmmss"];
}
- (void)setEndTime:(NSString *)endTime
{
    _endTime = endTime;
    self.endTimeGps = [CALENDARMGR dateStrWithDate:[CALENDARMGR dateWithStr:endTime format:@"yyyy-MM-dd HH:mm:ss"] format:@"yyyyMMddHHmmss"];
}
-(void)setStartGPS:(NSString *)startGPS{
    _startGPS = startGPS;
    if ([startGPS isNotNil]) {
        NSArray *gps = [startGPS componentsSeparatedByString:@","];
        //添加获取城市
        if (gps.count == 2) {
            CLLocationCoordinate2D coor = CLLocationCoordinate2DMake([gps[1] doubleValue], [gps[0] doubleValue]);
            __weak __typeof(self) wself = self;
            [self.mapSimpleStart reGeoCodeCoordinate:coor finish:^(NSString *poi, AMapReGeocode *reGeocode) {
                wself.startPlace = [MapHelper addressRoadFromReGeocode:reGeocode];
                [[NSNotificationCenter defaultCenter] postNotificationName:DRIVE_PLACE_UPDATE object:nil];
            } error:^{
            }];
        }
    }
}

-(NSString *)startPlace{
    return [_startPlace isNotNil]?_startPlace:@"未知";
}
- (NSString *)endPlace
{
    return [_endPlace isNotNil] ? _endPlace : @"未知";
}
-(void)setEndGPS:(NSString *)endGPS{
    _endGPS = endGPS;
    if ([endGPS isNotNil]) {
        NSArray *gps = [endGPS componentsSeparatedByString:@","];
        //添加获取城市
        if (gps.count == 2) {
            CLLocationCoordinate2D coor = CLLocationCoordinate2DMake([gps[1] doubleValue], [gps[0] doubleValue]);
            __weak __typeof(self) wself = self;
            [self.mapSimpleEnd reGeoCodeCoordinate:coor finish:^(NSString *poi, AMapReGeocode *reGeocode) {
                wself.endPlace = [MapHelper addressRoadFromReGeocode:reGeocode];
                [[NSNotificationCenter defaultCenter] postNotificationName:DRIVE_PLACE_UPDATE object:nil];
            } error:^{
            }];
        }
    }
}

-(NSString *)startAndEndTime{
    if (!_startAndEndTime) {
        if ([self.startTime isNotNil]) {
            NSString *start = [CALENDARMGR dateStrWithDate:[CALENDARMGR dateWithStr:self.startTime format:yyyy_MM_dd_HH_mm_ss] format:@"HH:mm"];
            NSString *end = [CALENDARMGR dateStrWithDate:[CALENDARMGR dateWithStr:self.endTime format:yyyy_MM_dd_HH_mm_ss] format:@"HH:mm"];
            _startAndEndTime = [NSString stringWithFormat:@"%@ - %@",start,end];
        }
    }
    return _startAndEndTime;
}

-(MapReGeocodeSimple *)mapSimpleStart{
    if (!_mapSimpleStart) {
        _mapSimpleStart = [[MapReGeocodeSimple alloc] init];
    }
    return _mapSimpleStart;
}

-(MapReGeocodeSimple *)mapSimpleEnd{
    if (!_mapSimpleEnd) {
        _mapSimpleEnd = [[MapReGeocodeSimple alloc] init];
    }
    return _mapSimpleEnd;
}

-(void)setHaTimes:(NSInteger)haTimes{
    _haTimes = haTimes;
    self.addSpeedControl = haTimes;
    if (haTimes > 5) {
        self.addSpeedControl = 5;
    }

}

-(void)setRdTimes:(NSInteger)rdTimes{
    _rdTimes = rdTimes;
    self.lowSpeedControl =  rdTimes;
    if (rdTimes > 5) {
        self.lowSpeedControl = 5;
    }
}

-(void)setSmooth:(NSInteger)smooth{
    _smooth = smooth;
    self.speedControl = smooth;
}



@end